package org.ihelpuoo.orm.handler.map;

import java.lang.reflect.Field;
import java.util.Map;

import org.ihelpuoo.orm.IIIException;
import org.ihelpuoo.orm.ObjectRule;
import org.ihelpuoo.orm.handler.ObjectProcessor;
import org.ihelpuoo.orm.handler.ReadObjectFieldHandler;

public class MapReadObjectFieldHandler extends ReadObjectFieldHandler
{

	public MapReadObjectFieldHandler(ObjectProcessor beanHandler)
	{
		super(beanHandler);
	}

	@Override
	protected Object evalSubObject(ObjectRule pr, Field f, Object src, Object subObj)
			throws Exception
	{
		String key = pr.getAlias(f);
		Object v = ((Map)src).get(key);
		if (null == v)
			return null;
		if (!(v instanceof Map))
		{
			throw new IIIException("Source object paire \"" + key + "\" should be a Map object!");
		}
		if (((Map)v).size() == 0)
			return null;
		ObjectRule subBr = processor.getManual().getRule(subObj.getClass());
		return processor.fetch(subBr,v,subObj,null,null, null);
	}

}
